package com.gxa.accommodation.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.accommodation.dto.AccommodationDTO;
import com.gxa.accommodation.entity.Accommodation;
import com.gxa.accommodation.entity.AccommodationRoomType;
import com.gxa.accommodation.mapper.AccommodationMapper;
import com.gxa.accommodation.service.AccommodationService;
import com.gxa.accommodation.vo.AccommodationRoomVO;
import com.gxa.common.base.RespResult;
import com.gxa.common.cofig.RedisConfig;
import com.gxa.common.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AccommodationServiceImpl implements AccommodationService {
    @Autowired
    AccommodationMapper accommodationMapper;

    @Autowired
    RedisTemplate redisTemplate;

    //存入缓存中的酒店和住宿 的KEY
    private static String ACCOMMODATION_KEY = "findAccommodation";

    @Override
    public RespResult addAccommodation(Accommodation accommodation) {
        log.info("进入添加酒店或住宿");

        int insert = accommodationMapper.insert(accommodation);
        if (insert > 0){
            //返回插入数据的id
            Long id = accommodation.getId();
            redisTemplate.delete(ACCOMMODATION_KEY);
            return RespResult.success(id);
        }else {
            return RespResult.error("添加失败");
        }
    }

    @Override
    public RespResult uploadaddAccommodationImg(MultipartFile file, Long id, HttpServletRequest request) {
        try{
            log.info("进入添加图片方法");
            Accommodation accommodation1 = accommodationMapper.selectById(id);
            String imgUrl = FileUtils.saveImage(file, request, FileUtils.ACCOMMODATION);

            FileUtils.deleteFile(accommodation1.getImg());

            Accommodation accommodation=new Accommodation();
            accommodation.setId(id);
            accommodation.setImg(imgUrl);
            accommodationMapper.updateById(accommodation);

            //清空ACCOMMODATION_KEY 的缓存
            redisTemplate.delete(ACCOMMODATION_KEY);
            return RespResult.success("添加图片成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("添加失败");
        }
    }

    @Override
    public RespResult findAccommodation(Integer currentPage,Integer pageSize) {
        log.info("进入查询酒店或住宿");
        if (currentPage==null||pageSize==null||currentPage<=0||pageSize<=0){
            currentPage=1;
            pageSize=10;
        }
        Page<AccommodationDTO> accommodationDTOS = null;
        //获取到redisTemplate对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //序列化redisTemplate模板
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        //从Redis获取到page
        accommodationDTOS = (Page<AccommodationDTO>) valueOperations.get(ACCOMMODATION_KEY);

        Page<AccommodationDTO> accommodationRoomPage = new Page<>(currentPage, pageSize);
        if (accommodationDTOS != null && currentPage == 1) {
            log.info("返回缓存中的结果");
            return RespResult.success(accommodationDTOS);
        } else if (accommodationDTOS == null && currentPage == 1) {

            accommodationDTOS = accommodationMapper.selectList(accommodationRoomPage);
            //存入缓存
            valueOperations.set(ACCOMMODATION_KEY, accommodationDTOS, 5, TimeUnit.MINUTES);
            return RespResult.success(accommodationDTOS);
        } else {
            accommodationDTOS = accommodationMapper.selectList(accommodationRoomPage);
            return RespResult.success(accommodationDTOS);
        }
    }
}
